package bill.study.serialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationTest {
	 public static void main(String[] args) {
//		 test(new EmployeeBean());
		 test(new EmployeeBean(new Employee("Ted Farrell")));

     }
	 
	 public static void test(EmployeeBean eBean){

         String filename = "bean.ser";

         File f = new File(filename);
         if(f.exists() && f.canWrite()){           
             if(f.delete())
                 System.out.println("Serilialized file deleted");
         }

         
         FileInputStream fis = null;
         ObjectInputStream in = null;
         FileOutputStream fos = null;
         ObjectOutputStream out = null;
         try {
             fos = new FileOutputStream(filename);
             out = new ObjectOutputStream(fos);
             out.writeObject(eBean);
             out.close();
            System.out.println("the employee bean being serialized is " + eBean);
            
            
             fis = new FileInputStream(filename);
             in = new ObjectInputStream(fis);
             EmployeeBean wbean=null;
             wbean = (EmployeeBean)in.readObject();
             in.close();
             System.out.println("the employee bean being de-serialized is " + wbean);
         } catch (IOException ex) {
             ex.printStackTrace();
         } catch (ClassNotFoundException ex) {
             ex.printStackTrace();
         }
	 }
}
